/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.system.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.wealth.system.entity.Permission;
import com.wealth.system.entity.Role;
import com.wealth.system.entity.User;
import com.wealth.system.repository.PermissionDao;
import com.wealth.system.repository.RoleDao;
import com.wealth.system.service.IPermissionService;
import com.wealth.system.vo.Tree;

import iaas.service.impl.BaseServiceImpl;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements IPermissionService{

	@Autowired
	private PermissionDao permissionDao;
	@Autowired
	private RoleDao roleDao;

	@Override
	public List<Permission> getPermissionByCorpIdAndStatus(Long corpid,Integer status) throws Exception {
		return permissionDao.findByCorpIdAndStatus(corpid, status);
	}

	@Override
	public List<Permission> getPermissionByCorpIdAndPidAndTypeAndStatus(Long corpid, Long pid,Integer type, Integer status) throws Exception {
		return permissionDao.findByCorpIdAndParentIdAndTypeAndStatus(corpid, pid, type, status);
	}

	@Override
	public List<Tree> getTree(User user)  throws Exception {
		
		List<Tree> trees = Lists.newArrayList();
//		Long corpid = user.getCorp().getId();
		Long userid = user.getId();
//		//超级管理员
//		if(user.getUsername().equals("admin")){
//			//查询根节点
//			List<Permission> permissionRootList =  permissionDao.findByCorpIdAndParentIdAndTypeAndStatus(corpid, 1L, PermissionTypeEnum.MENU.getValue(), StatusEnum.NORMAL.getValue());
//			if(permissionRootList==null){
//				return null;
//			}
//			//遍历一级节点
//			for (Permission permissionOne : permissionRootList) {
//				Tree treeOne = new Tree();
//				treeOne.setId(permissionOne.getId());
//				treeOne.setPid(permissionOne.getParent().getId());
//				treeOne.setName(permissionOne.getName());
//				treeOne.setUrl(permissionOne.getUrl());
//				treeOne.setSeq(permissionOne.getSeq());
//				treeOne.setMenuParent(permissionOne.getMenuParent());
//				treeOne.setMenuChild(permissionOne.getMenuChild());
//				
//				List<Permission> permissionChildList = permissionDao.findByCorpIdAndParentIdAndTypeAndStatus(corpid, permissionOne.getId(),  PermissionTypeEnum.MENU.getValue(), StatusEnum.NORMAL.getValue()) ;
//				if(permissionChildList!=null){
//					//遍历二级节点
//					List<Tree> treeList = Lists.newArrayList();
//					for (Permission permissionTwo : permissionChildList) {
//						Tree treeTwo = new Tree();
//						treeTwo.setId(permissionTwo.getId());
//						treeTwo.setPid(permissionTwo.getParent().getId());
//						treeTwo.setName(permissionTwo.getName());
//						treeTwo.setUrl(permissionTwo.getUrl());
//						treeTwo.setSeq(permissionTwo.getSeq());
//						treeTwo.setMenuParent(permissionTwo.getMenuParent());
//						treeTwo.setMenuChild(permissionTwo.getMenuChild());
//						treeList.add(treeTwo);
//					}
//					treeOne.setChildren(treeList);
//				}else{
//					treeOne.setState("closed");
//				}
//				trees.add(treeOne);
//			}
//			return trees;
//			//遍历结束
//		}
//		
		
		//普通用户
		Set<Permission> permissionIdList = Sets.newHashSet();
		List<Role> roleList = roleDao.findRoleListByUserId(userid);
		for (Role role : roleList) {
			permissionIdList.addAll(role.getPermissionList());
		}
		//遍历一级节点
		for (Permission permission : permissionIdList) {
			if (permission != null   && permission.getParent() != null  && permission.getParent().getId() == 1L  ) {
				Tree treeOne = new Tree();
				treeOne.setId(permission.getId());
				treeOne.setPid(permission.getParent().getId());
				treeOne.setName(permission.getName());
				treeOne.setUrl(permission.getUrl());
				treeOne.setSeq(permission.getSeq());
				treeOne.setMenuParent(permission.getMenuParent());
				treeOne.setMenuChild(permission.getMenuChild());
				
				List<Tree> treeList = Lists.newArrayList();
				for ( Permission permissionTwo : permissionIdList ) {
					if (permissionTwo.getParent()!=null  && permission.getId().longValue() == permissionTwo.getParent().getId().longValue() ) {
						Tree treeTwo = new Tree();
						treeTwo.setId(permissionTwo.getId());
						treeTwo.setPid(permissionTwo.getParent().getId());
						treeTwo.setName(permissionTwo.getName());
						treeTwo.setUrl(permissionTwo.getUrl());
						treeTwo.setSeq(permissionTwo.getSeq());
						treeTwo.setMenuParent(permissionTwo.getMenuParent());
						treeTwo.setMenuChild(permissionTwo.getMenuChild());
						treeList.add(treeTwo);
					}
				}
				Collections.sort(treeList);
				treeOne.setChildren(treeList);
				trees.add(treeOne);
			}
		}
		
		Collections.sort(trees);
		return trees;
		
		//遍历结束
		
	}
	
}
